//
//  ToolSysOpen.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

/// 系统设置的具体页面
public enum SystemSetType: String {
    
    case mainSet
    case WIFI
    case Bluetooth
    case Photos
    
    public var prefs: String {
        switch self {
        case .mainSet, .WIFI:
            return "App-Prefs:root=WIFI"
        case .Bluetooth, .Photos:
            if #available(iOS 16.0, *) {
                return UIApplication.openNotificationSettingsURLString
            } else {
                return ""
            }
        }
    }
}

public extension Tools {

    // 打开设置
    static public func openSet(_ type: SystemSetType) {
        guard let url = URL(string: type.prefs) else { return }
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
    
    /// 打电话
    static public func call(tel: String) -> Bool {
        guard let url = URL(string: "tel://\(tel)") else { return false }
        DispatchQueue.main.async {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
        return true
    }

    /// app跳转
    static public func appJump(urlScheme: String) {
        guard let url = URL(string: "\(urlScheme)://") else { return }
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}
